package cn.bugstack.middleware.rpcspringbootstarter.util;

import java.util.HashSet;
import java.util.Set;

/**
 * @author jie
 */
public class ClassLoaderUtils {

    private static Set<Class> primitiveSet = new HashSet<Class>();

    static {
        primitiveSet.add(Integer.class);
        primitiveSet.add(Long.class);
        primitiveSet.add(Float.class);
        primitiveSet.add(Byte.class);
        primitiveSet.add(Short.class);
        primitiveSet.add(Double.class);
        primitiveSet.add(Character.class);
        primitiveSet.add(Boolean.class);
    }

    /**
     * 得到当前 ClassLoader
     *
     * @return ClassLoader
     */
    public static ClassLoader getCurrentClassLoader(){
        ClassLoader cl = Thread.currentThread().getContextClassLoader();
        if (cl == null){
            cl = ClassLoaderUtils.class.getClassLoader();
        }
        return cl == null ? ClassLoader.getSystemClassLoader() : cl;
    }

    /**
     * 根据类名加载 Class
     *
     * @param className 类名
     * @return  Class
     * @throws ClassNotFoundException   找不到类
     */
    public static Class forName(String className) throws ClassNotFoundException {
        return forName(className, true);
    }

    /**
     * 根据类名加载 Class
     *
     * @param className 类名
     * @param initialize    是否初始化
     * @return  Class
     * @throws ClassNotFoundException   找不到类
     */
    public static Class forName(String className, boolean initialize) throws ClassNotFoundException {
        return Class.forName(className, initialize, getCurrentClassLoader());
    }


}
